#include "asycop.h"

AsyCop::AsyCop(QObject *parent):
    QObject(parent)
{
    mXml = new xmlHandle;
    mXmlData = new QByteArray;
    mShm = new ShmCop;
    QObject::connect(mShm,SIGNAL(dataArrived(const QByteArray &)),this,SLOT(onDataReceived(const QByteArray &)));
}

AsyCop::~AsyCop()
{
    if(mXml)
        delete mXml;
    if(mXmlData)
        delete mXmlData;
    if(mShm)
    {
        mShm->exit();
        delete mShm;
    }
}

int AsyCop::init()
{
    mShm->start();
}

int AsyCop::do_request(QString &cmd, QByteArray &arg)
{
    printf("receive remote call:%s\nargs:%s\n\n\n",cmd.toAscii().data(),arg.data());

}

int AsyCop::send_request(QString &cmd, QByteArray &arg,shmDirection d)
{
    mXml->cmdToXml(*mXmlData,cmd,arg);
    printf("call remote slot:%s\nargs:%s\n\n\n",cmd.toAscii().data(),arg.data());
    mShm->SendData(*mXmlData,d);
}

int AsyCop::do_timeout()
{
}

void AsyCop::onDataReceived(const QByteArray &data)
{
    QString cmd;
    QByteArray arg;
    mXml->xmlToCmd(data,cmd,arg);
    do_request(cmd,arg);
}


